<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">项目协作</div>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input size="medium" v-model="ruleForm.username" placeholder="用户名">
                        <i class="el-icon-lx-people" slot="prepend"></i>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input size="medium" type="password" placeholder="密码" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')">
                        <i class="el-icon-lx-lock" slot="prepend"></i>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import md5 from '@/utils/md5';
import bus from "@/components/common/bus";
export default {
    data: function(){
        return {
            ruleForm: {
                username: '',
                password: ''
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ]
            },
            loginOk:false,
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.request.post({
                        url: "user.login",
                        data: {
                            account:this.ruleForm.username,
                            password:md5.hex_md5('hjk0tpk*#en#&'+this.ruleForm.password)
                        },
                        needSign:true
                    }).then(res => {
                        this._loginOk(res)
                    }, res => {
                        this.$message.error(res.msg || "登录失败，请重试~");
                    });
                } else {
                    return false;
                }
            });
        },

        _loginOk(res) {
            this.loginOk = true;
            this.auth.setToken(res.result.token)
            this.auth.setAccount(res.result)

            //提前请求数据
            this.consts.users()
            this.consts.projects()

            this.$message.success({message:"登录成功！", duration:1000});
            this.$router.replace({path:"/"}).catch(()=>{})

            setTimeout(() => {
                bus.$emit('account_changed')
            }, 100)
        }
    }
}
</script>

<style scoped>
.login-wrap{
    position: relative;
    width:100%;
    height:100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: cover;
    background-repeat: no-repeat;
}
.ms-title{
    width:100%;
    line-height: 50px;
    text-align: center;
    font-size:20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login{
    position: absolute;
    left:50%;
    top:50%;
    width:350px;
    margin:-190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(0,0,0, 0.5);
    overflow: hidden;
}
.ms-content{
    padding: 30px 30px;
}
.login-btn{
    text-align: center;
}
.login-btn button{
    width:100%;
    height:36px;
    margin-bottom: 10px;
}

</style>
